package com.jianganming.netty.rpc.registry;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class RegistryInit {
    private ConcurrentHashMap<String, Object> registryMap = new ConcurrentHashMap<String,Object>();

    //保存所有相关的服务类
    private List<String> classNames = new ArrayList<String>();

    private Integer port;

    public RegistryInit(Integer port){
        this.port = port;
        //完成递归扫描
        scannerClass("com.jianganming.netty.rpc.provider");
        doRegister();
    }

    public ConcurrentHashMap<String, Object> getRegistryMap() {
        return registryMap;
    }

    public Integer getPort() {
        return port;
    }

    private void doRegister() {
        RegistryCenterWithZk registryCenterWithZk = RegistryCenterWithZk.getRegistryCenterWithZk();
        if(classNames.isEmpty())
            return;
        for (String className : classNames) {
            try {
                Class<?> clazz = Class.forName(className);
                Class<?> interClazz = clazz.getInterfaces()[0];
                Object o = clazz.newInstance();
                registryMap.put(interClazz.getName(),o);
                System.out.println(o);
                registryCenterWithZk.registry(interClazz.getName(),getHostIp()+":"+port);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void scannerClass(String packageName) {
        String path = packageName.replaceAll("\\.", "/");
        URL url = this.getClass().getClassLoader().getResource(path);
        File dir = new File(url.getFile());
        for (File file : dir.listFiles()) {
            if(file.isDirectory()){
                scannerClass(packageName+"."+file.getName());
            }else {
                classNames.add(packageName+"."+file.getName().replace(".class","").trim());
            }
        }
    }

    private static String getHostIp1() {

        try {
            InetAddress address = InetAddress.getLocalHost();
            return address.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getHostIp() {

        String sIP = "";
        InetAddress ip = null;
        try {
            boolean bFindIP = false;
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                if (bFindIP)
                    break;
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = ips.nextElement();
                    if (!ip.isLoopbackAddress()
                            && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
                        bFindIP = true;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != ip)
            sIP = ip.getHostAddress();
        return sIP;
    }


}
